package com.zy.rpc.common.balance;

import com.zy.rpc.common.Serializer.Serializer;
import com.zy.rpc.common.Serializer.impl.JavaSerializer;
import com.zy.rpc.common.Serializer.impl.JsonSerializer;
import com.zy.rpc.common.Serializer.impl.ProtobufSerializer;
import com.zy.rpc.common.balance.impl.FullRoundBalance;
import com.zy.rpc.common.balance.impl.RandomBalance;
import org.springframework.beans.factory.annotation.Value;

import javax.annotation.PostConstruct;
import java.util.HashMap;

/**
 * @author zy
 * @date 2023/8/31 17:35
 */
public class LoadBalanceFactory {
    private static final HashMap<String, LoadBalance> map = new HashMap<>();
    @Value("${loadBalance.defaultLoadBalanceName}")
    private static String defaultLoadBalanceName;
    @PostConstruct
    private void init() {
        map.put("fullRound", new FullRoundBalance());
        map.put("random", new RandomBalance());

    }
    private static   LoadBalance getLoadBalance(String loadBalanceName) {
        LoadBalance loadBalance = map.get(loadBalanceName);
        if (loadBalance == null) {
            throw new IllegalArgumentException("Unknown serialization type: " + loadBalanceName);
        }
        return loadBalance;
    }
    public static LoadBalance getDefaultLoadBalance() {
        return getLoadBalance(defaultLoadBalanceName);
    }

    public static void registerCustomLoadBalance(String name, LoadBalance customLoadBalance) {
        map.put(name, customLoadBalance);
    }
}
